package com.jing.yao.config.converter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jing.yao.config.threadlocal.ThreadLocalStore;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.TimeZone;

/**
 * Json转化器，解决国际化问题
 * @author lr
 * @since 2020-07-03
 */
public class YaoJacksonMessageConverter extends MappingJackson2HttpMessageConverter {

    public YaoJacksonMessageConverter(ObjectMapper objectMapper) {
        super(objectMapper);
    }

    @Override
    protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
        //获取当前线程的时区
        TimeZone timeZone = ThreadLocalStore.timeZoneThreadLocal.get();
        ObjectMapper objectMapper = getObjectMapper();
        objectMapper.setTimeZone(timeZone);
        super.setObjectMapper(objectMapper);
        super.writeInternal(object, type, outputMessage);
    }
}
